package chat.network.http;

import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.whu.administrator.ws.MyApplication;
import com.whu.administrator.ws.R;
import com.whu.administrator.ws.VolleySingleton;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import inter.Delegate;

/**创建房间
 * Created by 凯 on 2015/10/18.
 */
public class CreateRoom {

    Delegate delegate;

    UserCreateRoom userCreateRoom;

    public void setDelegate(Delegate delegate) {
        this.delegate = delegate;
    }

    public void setUserCreateRoom(UserCreateRoom userCreateRoom) {
        this.userCreateRoom = userCreateRoom;
    }

    public CreateRoom() {
    }

    public void create()
    {
        String IP = MyApplication.share().getResources().getString(R.string.IP);
        String url ="http://"+IP+"/Chat/CreateRoom";
        StringRequest stringRequest = new StringRequest(Request.Method.POST,url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        System.out.println(response);
                        if (delegate != null)
                            delegate.onResponse(response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (delegate != null)
                    delegate.onErrorResponse(error);
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map map = new HashMap();
                Gson gson = new Gson();
                map.put("data", gson.toJson(userCreateRoom));
                return  map;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String temp = null;
                try {
                    temp = new String(response.data,"utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }
        };
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }

    public static class UserCreateRoom {
        private Long user_ID;
        private String user_RealName;
        private String room_Name;
        private String room_Topic;
        private int room_MaxNum;
        private String room_CreateTime;
        private String room_Desc;

        public Long getUser_ID() {
            return user_ID;
        }
        public void setUser_ID(Long user_ID) {
            this.user_ID = user_ID;
        }

        public String getUser_RealName() {
            return user_RealName;
        }
        public void setUser_Name(String user_RealName) {
            this.user_RealName = user_RealName;
        }
        public String getRoom_Name() {
            return room_Name;
        }
        public void setRoom_Name(String room_Name) {
            this.room_Name = room_Name;
        }
        public String getRoom_Topic() {
            return room_Topic;
        }
        public void setRoom_Topic(String room_Topic) {
            this.room_Topic = room_Topic;
        }
        public int getRoom_MaxNum() {
            return room_MaxNum;
        }
        public void setRoom_MaxNum(int room_MaxNum) {
            this.room_MaxNum = room_MaxNum;
        }
        public String getRoom_CreateTime() {
            return room_CreateTime;
        }
        public void setRoom_CreateTime(String room_CreateTime) {
            this.room_CreateTime = room_CreateTime;
        }
        public String getRoom_Desc() {
            return room_Desc;
        }
        public void setRoom_Desc(String room_Desc) {
            this.room_Desc = room_Desc;
        }
    }

    public static class ResponseCreateRoom {
        private String status;

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

    }

}
